package main;

import infor.User;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import login.Connects;
import login.Login;

import javax.swing.JTextField;
import java.awt.SystemColor;

public class ChangePass extends JFrame {
	static ChangePass frame;
	private JPanel contentPane;
	private JPasswordField pasCurrent;
	private JPasswordField pasNew;
	private JPasswordField pasConfirm;
	public static String code, author;
	public String cur_pass, table = null, matkhau = null, ma = null;
	Home h;
	Login l;
	Connects cn = new Connects();
	Connection cnn = cn.con;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new ChangePass();
					frame.setVisible(true);
					frame.setTitle("[User]: ");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void author_switch() {
		if (author == "Admin") {
			table = "login";
			ma = "username";
			matkhau = "password";
		}
		if (author == "Nhân viên") {
			table = "nhanvien";
			ma = "MaNV";
			matkhau = "MatKhauNV";
		}
		if (author == "Khách hàng") {
			table = "khachhang";
			ma = "MaKH";
			matkhau = "MatKhauKH";
		}
	}

	public void clear() {
		pasCurrent.setText("");
		pasNew.setText("");
		pasConfirm.setText("");

	}

	public void currentpass() throws Exception {
		author_switch();
		Statement stt = cnn.createStatement();
		String sqlCur = "select " + matkhau + " from " + table + " where "
				+ matkhau + "='" + pasCurrent.getText().toString() + "'";
		stt.execute(sqlCur);
		ResultSet rs = stt.getResultSet();
		if (rs.next() == false) {
			JOptionPane.showMessageDialog(this, "Mật khẩu cũ không đúng");
			System.out.print("Inputpass: " + pasCurrent.getText());
			clear();
		}

		else {
			if (pasNew.getText().toString()
					.equals(pasConfirm.getText().toString()))
				changePass();
			else
				JOptionPane.showMessageDialog(this,
						"Xác nhận mật khẩu không khớp");
			pasNew.setText("");
			pasConfirm.setText("");
		}

	}

	public void changePass() throws Exception {
		Connects cn = new Connects();
		Connection cnn = cn.con;

		author_switch();
		h = new Home();
		try {
			Statement stt = cnn.createStatement();
			String sua = "update " + table + " set " + matkhau + "='"
					+ pasNew.getText().toString() + "' where " + ma + "='"
					+ code + "'";
			stt.execute(sua);

			JOptionPane.showMessageDialog(ChangePass.this,
					"Đổi mật khẩu thành công cho " + author + " " + code);
			System.out.println("Author: " + author + "\nCode: " + code
					+ "\nNew Password: " + pasNew.getText().toString());
			clear();
			dispose();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			JOptionPane.showMessageDialog(ChangePass.this,
					"Đổi mật khẩu không thành công");
			System.out.print("New Password Failed");

		}
	}

	/**
	 * Create the frame.
	 */
	public ChangePass() {
		setBounds(100, 100, 330, 159);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblMtKhuC = new JLabel("Mật khẩu cũ:");
		lblMtKhuC.setBounds(10, 14, 75, 14);
		contentPane.add(lblMtKhuC);

		JLabel lblMtKhuMi = new JLabel("Mật khẩu mới:");
		lblMtKhuMi.setBounds(10, 38, 87, 14);
		contentPane.add(lblMtKhuMi);

		JLabel lblXcNhnMt = new JLabel("Xác nhận mật khẩu:");
		lblXcNhnMt.setBounds(10, 63, 124, 14);
		contentPane.add(lblXcNhnMt);

		pasCurrent = new JPasswordField();
		pasCurrent.setBounds(120, 11, 180, 20);
		contentPane.add(pasCurrent);

		pasNew = new JPasswordField();
		pasNew.setBounds(120, 35, 180, 20);
		contentPane.add(pasNew);

		pasConfirm = new JPasswordField();
		pasConfirm.setBounds(120, 60, 180, 20);
		contentPane.add(pasConfirm);

		JButton btnOk = new JButton("OK");
		btnOk.setBounds(164, 88, 61, 23);
		contentPane.add(btnOk);
		btnOk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					currentpass();

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		JButton btnDong = new JButton("Đóng");
		btnDong.setBounds(235, 88, 69, 23);
		contentPane.add(btnDong);

		btnDong.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();

			}
		});
	}

}
